<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>functions_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_functions_functions_test.js.html">functions_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.functions.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.functionsTest&#39;);
<a name="line20"></a>goog.setTestOnly(&#39;goog.functionsTest&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.functions&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>var fTrue = makeCallOrderLogger(&#39;fTrue&#39;, true);
<a name="line29"></a>var gFalse = makeCallOrderLogger(&#39;gFalse&#39;, false);
<a name="line30"></a>var hTrue = makeCallOrderLogger(&#39;hTrue&#39;, true);
<a name="line31"></a>
<a name="line32"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line33"></a>
<a name="line34"></a>function setUp() {
<a name="line35"></a>  callOrder = [];
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function tearDown() {
<a name="line39"></a>  stubs.reset();
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>function testTrue() {
<a name="line43"></a>  assertTrue(goog.functions.TRUE());
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function testFalse() {
<a name="line47"></a>  assertFalse(goog.functions.FALSE());
<a name="line48"></a>}
<a name="line49"></a>
<a name="line50"></a>function testLock() {
<a name="line51"></a>  function add(var_args) {
<a name="line52"></a>    var result = 0;
<a name="line53"></a>    for (var i = 0; i &lt; arguments.length; i++) {
<a name="line54"></a>      result += arguments[i];
<a name="line55"></a>    }
<a name="line56"></a>    return result;
<a name="line57"></a>  }
<a name="line58"></a>
<a name="line59"></a>  assertEquals(6, add(1, 2, 3));
<a name="line60"></a>  assertEquals(0, goog.functions.lock(add)(1, 2, 3));
<a name="line61"></a>  assertEquals(3, goog.functions.lock(add, 2)(1, 2, 3));
<a name="line62"></a>  assertEquals(6, goog.partial(add, 1, 2)(3));
<a name="line63"></a>  assertEquals(3, goog.functions.lock(goog.partial(add, 1, 2))(3));
<a name="line64"></a>}
<a name="line65"></a>
<a name="line66"></a>function testNth() {
<a name="line67"></a>  assertEquals(1, goog.functions.nth(0)(1));
<a name="line68"></a>  assertEquals(2, goog.functions.nth(1)(1, 2));
<a name="line69"></a>  assertEquals(&#39;a&#39;, goog.functions.nth(0)(&#39;a&#39;, &#39;b&#39;));
<a name="line70"></a>  assertEquals(undefined, goog.functions.nth(0)());
<a name="line71"></a>  assertEquals(undefined, goog.functions.nth(1)(true));
<a name="line72"></a>  assertEquals(undefined, goog.functions.nth(-1)());
<a name="line73"></a>}
<a name="line74"></a>
<a name="line75"></a>function testIdentity() {
<a name="line76"></a>  assertEquals(3, goog.functions.identity(3));
<a name="line77"></a>  assertEquals(3, goog.functions.identity(3, 4, 5, 6));
<a name="line78"></a>  assertEquals(&#39;Hi there&#39;, goog.functions.identity(&#39;Hi there&#39;));
<a name="line79"></a>  assertEquals(null, goog.functions.identity(null));
<a name="line80"></a>  assertEquals(undefined, goog.functions.identity());
<a name="line81"></a>
<a name="line82"></a>  var arr = [1, &#39;b&#39;, null];
<a name="line83"></a>  assertEquals(arr, goog.functions.identity(arr));
<a name="line84"></a>  var obj = {a: &#39;ay&#39;, b: &#39;bee&#39;, c: &#39;see&#39;};
<a name="line85"></a>  assertEquals(obj, goog.functions.identity(obj));
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testConstant() {
<a name="line89"></a>  assertEquals(3, goog.functions.constant(3)());
<a name="line90"></a>  assertEquals(undefined, goog.functions.constant()());
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function testError() {
<a name="line94"></a>  var f = goog.functions.error(&#39;x&#39;);
<a name="line95"></a>  var e = assertThrows(
<a name="line96"></a>      &#39;A function created by goog.functions.error must throw an error&#39;, f);
<a name="line97"></a>  assertEquals(&#39;x&#39;, e.message);
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>function testFail() {
<a name="line101"></a>  var obj = {};
<a name="line102"></a>  var f = goog.functions.fail(obj);
<a name="line103"></a>  var e = assertThrows(
<a name="line104"></a>      &#39;A function created by goog.functions.raise must throw its input&#39;, f);
<a name="line105"></a>  assertEquals(obj, e);
<a name="line106"></a>}
<a name="line107"></a>
<a name="line108"></a>function testCompose() {
<a name="line109"></a>  var add2 = function(x) {
<a name="line110"></a>    return x + 2;
<a name="line111"></a>  };
<a name="line112"></a>
<a name="line113"></a>  var doubleValue = function(x) {
<a name="line114"></a>    return x * 2;
<a name="line115"></a>  };
<a name="line116"></a>
<a name="line117"></a>  assertEquals(6, goog.functions.compose(doubleValue, add2)(1));
<a name="line118"></a>  assertEquals(4, goog.functions.compose(add2, doubleValue)(1));
<a name="line119"></a>  assertEquals(6, goog.functions.compose(add2, add2, doubleValue)(1));
<a name="line120"></a>  assertEquals(12,
<a name="line121"></a>      goog.functions.compose(doubleValue, add2, add2, doubleValue)(1));
<a name="line122"></a>  assertUndefined(goog.functions.compose()(1));
<a name="line123"></a>  assertEquals(3, goog.functions.compose(add2)(1));
<a name="line124"></a>
<a name="line125"></a>  var add2Numbers = function(x, y) {
<a name="line126"></a>    return x + y;
<a name="line127"></a>  };
<a name="line128"></a>  assertEquals(17, goog.functions.compose(add2Numbers)(10, 7));
<a name="line129"></a>  assertEquals(34, goog.functions.compose(doubleValue, add2Numbers)(10, 7));
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testAdd() {
<a name="line133"></a>  assertUndefined(goog.functions.sequence()());
<a name="line134"></a>  assertCallOrderAndReset([]);
<a name="line135"></a>
<a name="line136"></a>  assert(goog.functions.sequence(fTrue)());
<a name="line137"></a>  assertCallOrderAndReset([&#39;fTrue&#39;]);
<a name="line138"></a>
<a name="line139"></a>  assertFalse(goog.functions.sequence(fTrue, gFalse)());
<a name="line140"></a>  assertCallOrderAndReset([&#39;fTrue&#39;, &#39;gFalse&#39;]);
<a name="line141"></a>
<a name="line142"></a>  assert(goog.functions.sequence(fTrue, gFalse, hTrue)());
<a name="line143"></a>  assertCallOrderAndReset([&#39;fTrue&#39;, &#39;gFalse&#39;, &#39;hTrue&#39;]);
<a name="line144"></a>
<a name="line145"></a>  assert(goog.functions.sequence(goog.functions.identity)(true));
<a name="line146"></a>  assertFalse(goog.functions.sequence(goog.functions.identity)(false));
<a name="line147"></a>}
<a name="line148"></a>
<a name="line149"></a>function testAnd() {
<a name="line150"></a>  // the return value is unspecified for an empty and
<a name="line151"></a>  goog.functions.and()();
<a name="line152"></a>  assertCallOrderAndReset([]);
<a name="line153"></a>
<a name="line154"></a>  assert(goog.functions.and(fTrue)());
<a name="line155"></a>  assertCallOrderAndReset([&#39;fTrue&#39;]);
<a name="line156"></a>
<a name="line157"></a>  assertFalse(goog.functions.and(fTrue, gFalse)());
<a name="line158"></a>  assertCallOrderAndReset([&#39;fTrue&#39;, &#39;gFalse&#39;]);
<a name="line159"></a>
<a name="line160"></a>  assertFalse(goog.functions.and(fTrue, gFalse, hTrue)());
<a name="line161"></a>  assertCallOrderAndReset([&#39;fTrue&#39;, &#39;gFalse&#39;]);
<a name="line162"></a>
<a name="line163"></a>  assert(goog.functions.and(goog.functions.identity)(true));
<a name="line164"></a>  assertFalse(goog.functions.and(goog.functions.identity)(false));
<a name="line165"></a>}
<a name="line166"></a>
<a name="line167"></a>function testOr() {
<a name="line168"></a>  // the return value is unspecified for an empty or
<a name="line169"></a>  goog.functions.or()();
<a name="line170"></a>  assertCallOrderAndReset([]);
<a name="line171"></a>
<a name="line172"></a>  assert(goog.functions.or(fTrue)());
<a name="line173"></a>  assertCallOrderAndReset([&#39;fTrue&#39;]);
<a name="line174"></a>
<a name="line175"></a>  assert(goog.functions.or(fTrue, gFalse)());
<a name="line176"></a>  assertCallOrderAndReset([&#39;fTrue&#39;]);
<a name="line177"></a>
<a name="line178"></a>  assert(goog.functions.or(fTrue, gFalse, hTrue)());
<a name="line179"></a>  assertCallOrderAndReset([&#39;fTrue&#39;]);
<a name="line180"></a>
<a name="line181"></a>  assert(goog.functions.or(goog.functions.identity)(true));
<a name="line182"></a>  assertFalse(goog.functions.or(goog.functions.identity)(false));
<a name="line183"></a>}
<a name="line184"></a>
<a name="line185"></a>function testNot() {
<a name="line186"></a>  assertTrue(goog.functions.not(gFalse)());
<a name="line187"></a>  assertCallOrderAndReset([&#39;gFalse&#39;]);
<a name="line188"></a>
<a name="line189"></a>  assertTrue(goog.functions.not(goog.functions.identity)(false));
<a name="line190"></a>  assertFalse(goog.functions.not(goog.functions.identity)(true));
<a name="line191"></a>
<a name="line192"></a>  var f = function(a, b) {
<a name="line193"></a>    assertEquals(1, a);
<a name="line194"></a>    assertEquals(2, b);
<a name="line195"></a>    return false;
<a name="line196"></a>  };
<a name="line197"></a>
<a name="line198"></a>  assertTrue(goog.functions.not(f)(1, 2));
<a name="line199"></a>}
<a name="line200"></a>
<a name="line201"></a>function testCreate(expectedArray) {
<a name="line202"></a>  var tempConstructor = function(a, b) {
<a name="line203"></a>    this.foo = a;
<a name="line204"></a>    this.bar = b;
<a name="line205"></a>  };
<a name="line206"></a>
<a name="line207"></a>  var factory = goog.partial(goog.functions.create, tempConstructor, &#39;baz&#39;);
<a name="line208"></a>  var instance = factory(&#39;qux&#39;);
<a name="line209"></a>
<a name="line210"></a>  assert(instance instanceof tempConstructor);
<a name="line211"></a>  assertEquals(instance.foo, &#39;baz&#39;);
<a name="line212"></a>  assertEquals(instance.bar, &#39;qux&#39;);
<a name="line213"></a>}
<a name="line214"></a>
<a name="line215"></a>function testWithReturnValue() {
<a name="line216"></a>  var obj = {};
<a name="line217"></a>  var f = function(a, b) {
<a name="line218"></a>    assertEquals(obj, this);
<a name="line219"></a>    assertEquals(1, a);
<a name="line220"></a>    assertEquals(2, b);
<a name="line221"></a>  };
<a name="line222"></a>  assertTrue(goog.functions.withReturnValue(f, true).call(obj, 1, 2));
<a name="line223"></a>  assertFalse(goog.functions.withReturnValue(f, false).call(obj, 1, 2));
<a name="line224"></a>}
<a name="line225"></a>
<a name="line226"></a>function makeCallOrderLogger(name, returnValue) {
<a name="line227"></a>  return function() {
<a name="line228"></a>    callOrder.push(name);
<a name="line229"></a>    return returnValue;
<a name="line230"></a>  };
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>function assertCallOrderAndReset(expectedArray) {
<a name="line234"></a>  assertArrayEquals(expectedArray, callOrder);
<a name="line235"></a>  callOrder = [];
<a name="line236"></a>}
<a name="line237"></a>
<a name="line238"></a>function testCacheReturnValue() {
<a name="line239"></a>  var returnFive = function() {
<a name="line240"></a>    return 5;
<a name="line241"></a>  };
<a name="line242"></a>
<a name="line243"></a>  var recordedReturnFive = goog.testing.recordFunction(returnFive);
<a name="line244"></a>  var cachedRecordedReturnFive = goog.functions.cacheReturnValue(
<a name="line245"></a>      recordedReturnFive);
<a name="line246"></a>
<a name="line247"></a>  assertEquals(0, recordedReturnFive.getCallCount());
<a name="line248"></a>  assertEquals(5, cachedRecordedReturnFive());
<a name="line249"></a>  assertEquals(1, recordedReturnFive.getCallCount());
<a name="line250"></a>  assertEquals(5, cachedRecordedReturnFive());
<a name="line251"></a>  assertEquals(1, recordedReturnFive.getCallCount());
<a name="line252"></a>}
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>function testCacheReturnValueFlagEnabled() {
<a name="line256"></a>  var count = 0;
<a name="line257"></a>  var returnIncrementingInteger = function() {
<a name="line258"></a>    count++;
<a name="line259"></a>    return count;
<a name="line260"></a>  };
<a name="line261"></a>
<a name="line262"></a>  var recordedFunction = goog.testing.recordFunction(
<a name="line263"></a>      returnIncrementingInteger);
<a name="line264"></a>  var cachedRecordedFunction = goog.functions.cacheReturnValue(
<a name="line265"></a>      recordedFunction);
<a name="line266"></a>
<a name="line267"></a>  assertEquals(0, recordedFunction.getCallCount());
<a name="line268"></a>  assertEquals(1, cachedRecordedFunction());
<a name="line269"></a>  assertEquals(1, recordedFunction.getCallCount());
<a name="line270"></a>  assertEquals(1, cachedRecordedFunction());
<a name="line271"></a>  assertEquals(1, recordedFunction.getCallCount());
<a name="line272"></a>  assertEquals(1, cachedRecordedFunction());
<a name="line273"></a>}
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>function testCacheReturnValueFlagDisabled() {
<a name="line277"></a>  stubs.set(goog.functions, &#39;CACHE_RETURN_VALUE&#39;, false);
<a name="line278"></a>
<a name="line279"></a>  var count = 0;
<a name="line280"></a>  var returnIncrementingInteger = function() {
<a name="line281"></a>    count++;
<a name="line282"></a>    return count;
<a name="line283"></a>  };
<a name="line284"></a>
<a name="line285"></a>  var recordedFunction = goog.testing.recordFunction(
<a name="line286"></a>      returnIncrementingInteger);
<a name="line287"></a>  var cachedRecordedFunction = goog.functions.cacheReturnValue(
<a name="line288"></a>      recordedFunction);
<a name="line289"></a>
<a name="line290"></a>  assertEquals(0, recordedFunction.getCallCount());
<a name="line291"></a>  assertEquals(1, cachedRecordedFunction());
<a name="line292"></a>  assertEquals(1, recordedFunction.getCallCount());
<a name="line293"></a>  assertEquals(2, cachedRecordedFunction());
<a name="line294"></a>  assertEquals(2, recordedFunction.getCallCount());
<a name="line295"></a>  assertEquals(3, cachedRecordedFunction());
<a name="line296"></a>}
</pre>


</body>
</html>
